package com.life.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.life.constants.Constants;

public class RuleConfigurationPanel extends JPanel 
{
	JPanel probabilisticRulesPanel = null;
	JPanel userDefinedRulesPanel = null;
	JPanel predefinedRulesPanel = null;
	public static void main(String args[])
	{
		JFrame frame = new JFrame();
		
		frame.setBounds(0,0,300,600);
		frame.getContentPane().setBounds(0,0,300,600);

		RuleConfigurationPanel p = new RuleConfigurationPanel();
		frame.getContentPane().add(p);
		frame.setVisible(true);		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	WidgetRegistry widgetRegistry = null;
	
	public RuleConfigurationPanel()
	{
		widgetRegistry = WidgetRegistry.getInstance();		
		init();
	}
	
	protected void init()
	{
		
		
		JTabbedPane mainTabbedPane = new JTabbedPane();
		JTabbedPane subPane = new JTabbedPane();
	
		
		JPanel definedPanel = new JPanel();
		JPanel probabilisticPanel =  new JPanel();
		
		JPanel optionsPanel = new JPanel();
		TitledBorder selectRuleMethod = new TitledBorder("Method");
		optionsPanel.setBorder(selectRuleMethod);
		optionsPanel.setLayout(null);
		
		JRadioButton usePredefined = new JRadioButton("Predefined");
		usePredefined.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				System.out.println("Action Performed");
				probabilisticRulesPanel.setVisible(false);
				userDefinedRulesPanel.setVisible(false);
				predefinedRulesPanel.setVisible(true);
			}
		});
		
		usePredefined.setSelected(true);
	
		
		usePredefined.setBounds(15,25,100,15);
		JRadioButton useUserDefined = new JRadioButton("User Defined");
		
		useUserDefined.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				System.out.println("Action Performed");
				probabilisticRulesPanel.setVisible(false);
				userDefinedRulesPanel.setVisible(true);
				predefinedRulesPanel.setVisible(false);
			}
		});
		
		useUserDefined.setBounds(15,45,100,15);
		JRadioButton useProbabilistic = new JRadioButton("Probabilistic");
		useProbabilistic.setBounds(15,65,100,15);
		
		useProbabilistic.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				System.out.println("Action Performed");
				probabilisticRulesPanel.setVisible(true);
				userDefinedRulesPanel.setVisible(false);
				predefinedRulesPanel.setVisible(false);
			}
		});
		
		JButton applyButton = new JButton("Apply");
		applyButton.setBounds(150,25,95,55);
		
		widgetRegistry.put(Constants.RULES_APPLY_WIDGET, applyButton);
		
		widgetRegistry.put(Constants.USE_PRBL_DEFINED_WIDGET,useProbabilistic);
		widgetRegistry.put(Constants.USE_USER_DEFINED_WIDGET,useUserDefined);
		widgetRegistry.put(Constants.USE_PREDEFINED_WIDGET,usePredefined);
				
		optionsPanel.add(applyButton);
		
		ButtonGroup optionsGroup = new ButtonGroup();
		optionsGroup.add(usePredefined);
		optionsGroup.add(useUserDefined);
		optionsGroup.add(useProbabilistic);
		
		optionsPanel.add(usePredefined);
		optionsPanel.add(useUserDefined);
		optionsPanel.add(useProbabilistic);
		
		
		definedPanel.setLayout(new GridLayout(2,1));
		
		
		TitledBorder predefinedRulesTitle = new TitledBorder("Predefined");
		predefinedRulesPanel = new JPanel();
		predefinedRulesPanel.setBorder(predefinedRulesTitle);
		
		
		predefinedRulesPanel.setLayout(new GridLayout(4,3));
		
		ButtonGroup group = new ButtonGroup();
		JRadioButton preset1 = new JRadioButton("");
		JRadioButton preset2 = new JRadioButton("");
		JRadioButton preset3 = new JRadioButton("");
		JRadioButton preset4 = new JRadioButton("");
		JRadioButton preset5 = new JRadioButton("");
		JRadioButton preset6 = new JRadioButton("");
		JRadioButton preset7 = new JRadioButton("");
		JRadioButton preset8 = new JRadioButton("");
		JRadioButton preset9 = new JRadioButton("");
		JRadioButton preset10 = new JRadioButton("");
		JRadioButton preset11 = new JRadioButton("");
		JRadioButton preset12 = new JRadioButton("");
		
		group.add(preset1);
		group.add(preset2);
		group.add(preset3);
		group.add(preset4);
		group.add(preset5);
		group.add(preset6);
		group.add(preset7);
		group.add(preset8);
		group.add(preset9);
		group.add(preset10);
		group.add(preset11);
		group.add(preset12);
		
		
		
		predefinedRulesPanel.add(preset1);
		predefinedRulesPanel.add(preset2);
		predefinedRulesPanel.add(preset3);
		predefinedRulesPanel.add(preset4);
		predefinedRulesPanel.add(preset5);
		predefinedRulesPanel.add(preset6);
		predefinedRulesPanel.add(preset7);
		predefinedRulesPanel.add(preset8);
		predefinedRulesPanel.add(preset9);
		predefinedRulesPanel.add(preset10);
		predefinedRulesPanel.add(preset11);
		predefinedRulesPanel.add(preset12);
		
		TitledBorder userDefinedRulesTitle = new TitledBorder("Survival/Birth");
		 userDefinedRulesPanel = new JPanel();
		userDefinedRulesPanel.setBorder(userDefinedRulesTitle);

		userDefinedRulesPanel.setLayout(new GridBagLayout());
	
		JLabel survivalLabel = new JLabel("Survival Rate");
		GridBagConstraints constraints1 = new GridBagConstraints();
		constraints1.anchor = GridBagConstraints.CENTER;
		constraints1.gridx = 0;
		constraints1.gridy = 0;
		constraints1.ipadx = 100;
		
		JLabel birthLabel = new JLabel("Birth Rate");
		GridBagConstraints constraints2 = new GridBagConstraints();
		constraints2.anchor = GridBagConstraints.CENTER;
		constraints2.gridx = 1;
		constraints2.gridy = 0;
		constraints2.ipadx = 100;
		
		JTextField survivalTextField = new JTextField();
		
		GridBagConstraints constraints3 = new GridBagConstraints();
		constraints3.anchor = GridBagConstraints.CENTER;
		constraints3.gridx = 0;
		constraints3.gridy = 1;
		constraints3.ipadx = 100;
		
		JTextField birthTextField = new JTextField();	
		
		GridBagConstraints constraints4 = new GridBagConstraints();
		constraints4.anchor = GridBagConstraints.CENTER;
		constraints4.gridx = 1;
		constraints4.gridy = 1;
		constraints4.ipadx = 100;
		
		userDefinedRulesPanel.add(survivalLabel,constraints1);
		userDefinedRulesPanel.add(birthLabel,constraints3);
		userDefinedRulesPanel.add(survivalTextField,constraints2);
		userDefinedRulesPanel.add(birthTextField,constraints4);
		
		TitledBorder probabilisticRulesTitle = new TitledBorder("Probabilistic");
		 probabilisticRulesPanel = new JPanel();
		probabilisticRulesPanel.setBorder(probabilisticRulesTitle);
		probabilisticRulesPanel.setLayout(new FlowLayout());
		
	
		
		JLabel label1 = new JLabel("Specify the probability of Survival and Birth");
		//probabilisticRulesPanel.add(label1,BorderLayout.NORTH);
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new GridBagLayout());
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new GridBagLayout());
		
		TitledBorder survivalTitle = new TitledBorder("Survival");
		leftPanel.setBorder(survivalTitle);
		probabilisticRulesPanel.add(leftPanel,BorderLayout.WEST);
		
		TitledBorder birthTitle = new TitledBorder("Birth");
		rightPanel.setBorder(birthTitle);
		probabilisticRulesPanel.add(rightPanel,BorderLayout.CENTER);
		
		
		
		
		JLabel label0s = new JLabel("0");
	    JLabel label1s = new JLabel("1");
	    JLabel label2s = new JLabel("2");
	    JLabel label3s = new JLabel("3");
	    JLabel label4s = new JLabel("4");
	    JLabel label5s = new JLabel("5");
	    JLabel label6s = new JLabel("6");
	    JLabel label7s = new JLabel("7");
	    JLabel label8s = new JLabel("8");
	
	    
	    
		JTextField text0s = new JTextField("0");
	    JTextField text1s = new JTextField("0");
	    JTextField text2s = new JTextField("100");
	    JTextField text3s = new JTextField("100");
	    JTextField text4s = new JTextField("0");
	    JTextField text5s = new JTextField("0");
	    JTextField text6s = new JTextField("0");
	    JTextField text7s = new JTextField("0");
	    JTextField text8s = new JTextField("0");
		
	    
	    
	    
	    
	    
	    JTextField text0b = new JTextField("0");
	    JTextField text1b = new JTextField("0");
	    JTextField text2b = new JTextField("0");
	    JTextField text3b = new JTextField("100");
	    JTextField text4b = new JTextField("0");
	    JTextField text5b = new JTextField("0");
	    JTextField text6b = new JTextField("0");
	    JTextField text7b = new JTextField("0");
	    JTextField text8b = new JTextField("0");
	    
	    
	    JLabel label0b = new JLabel("0");
	    JLabel label1b = new JLabel("1");
	    JLabel label2b = new JLabel("2");
	    JLabel label3b = new JLabel("3");
	    JLabel label4b = new JLabel("4");
	    JLabel label5b = new JLabel("5");
	    JLabel label6b = new JLabel("6");
	    JLabel label7b = new JLabel("7");
	    JLabel label8b = new JLabel("8");
	
		
	    widgetRegistry.put(Constants.RADIO_GROUP_WIDGET, group);
	   	widgetRegistry.put(Constants. USER_DEFINED_SURVIVAL_WIDGET, survivalTextField);
	   	widgetRegistry.put(Constants.USER_DEFINED_BIRTH_WIDGET, birthTextField);
		
	     widgetRegistry.put(Constants.PREDEFINED_1_WIDGET,preset1);
	     widgetRegistry.put(Constants.PREDEFINED_2_WIDGET,preset2);
	     widgetRegistry.put(Constants.PREDEFINED_3_WIDGET,preset3);
	     widgetRegistry.put(Constants.PREDEFINED_4_WIDGET,preset4);
	     widgetRegistry.put(Constants.PREDEFINED_5_WIDGET,preset5);
	     widgetRegistry.put(Constants.PREDEFINED_6_WIDGET,preset6);
	     widgetRegistry.put(Constants.PREDEFINED_7_WIDGET,preset7);
	     widgetRegistry.put(Constants.PREDEFINED_8_WIDGET,preset8);
	     widgetRegistry.put(Constants.PREDEFINED_9_WIDGET,preset9);
	     widgetRegistry.put(Constants.PREDEFINED_10_WIDGET,preset10);
	     widgetRegistry.put(Constants.PREDEFINED_11_WIDGET,preset11);
	     widgetRegistry.put(Constants.PREDEFINED_12_WIDGET,preset12);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_0_PERCENT_WIDGET,text0s); 
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_1_PERCENT_WIDGET ,text1s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_2_PERCENT_WIDGET ,text2s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_3_PERCENT_WIDGET ,text3s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_4_PERCENT_WIDGET ,text4s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_5_PERCENT_WIDGET ,text5s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_6_PERCENT_WIDGET ,text6s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_7_PERCENT_WIDGET ,text7s);
		 widgetRegistry.put(Constants.PRBL_SURVIVAL_8_PERCENT_WIDGET ,text8s);
		
		 widgetRegistry.put(Constants.PRBL_BIRTH_0_PERCENT_WIDGET,text0b); 
		 widgetRegistry.put(Constants.PRBL_BIRTH_1_PERCENT_WIDGET ,text1b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_2_PERCENT_WIDGET ,text2b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_3_PERCENT_WIDGET ,text3b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_4_PERCENT_WIDGET ,text4b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_5_PERCENT_WIDGET ,text5b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_6_PERCENT_WIDGET ,text6b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_7_PERCENT_WIDGET ,text7b);
		 widgetRegistry.put(Constants.PRBL_BIRTH_8_PERCENT_WIDGET ,text8b);
		
		
	    leftPanel.add(label0s,Constants.getConstraints(0,0,10));	    
	    leftPanel.add(text0s, Constants.getConstraints(1,0,50));
	    
	    leftPanel.add(label1s,Constants.getConstraints(0,1,10));
	    leftPanel.add(text1s,Constants.getConstraints(1,1,50));
	    
	    leftPanel.add(label2s,Constants.getConstraints(0,2,10));
	    leftPanel.add(text2s,Constants.getConstraints(1,2,50));
	    leftPanel.add(label3s,Constants.getConstraints(0,3,10));
	    leftPanel.add(text3s,Constants.getConstraints(1,3,50));
	    leftPanel.add(label4s,Constants.getConstraints(0,4,10));
	    leftPanel.add(text4s,Constants.getConstraints(1,4,50));
	    leftPanel.add(label5s,Constants.getConstraints(0,5,10));
	    leftPanel.add(text5s,Constants.getConstraints(1,5,50));
	    leftPanel.add(label6s,Constants.getConstraints(0,6,10));
	    leftPanel.add(text6s,Constants.getConstraints(1,6,50));
	    leftPanel.add(label7s,Constants.getConstraints(0,7,10));
	    leftPanel.add(text7s,Constants.getConstraints(1,7,50));
	    leftPanel.add(label8s,Constants.getConstraints(0,8,10));
	    leftPanel.add(text8s,Constants.getConstraints(1,8,50));
	    
	    
	    rightPanel.add(label0b,Constants.getConstraints(0,0,10));	    
	    rightPanel.add(text0b, Constants.getConstraints(1,0,50));
	    
	    rightPanel.add(label1b,Constants.getConstraints(0,1,10));
	    rightPanel.add(text1b,Constants.getConstraints(1,1,50));
	  
	    rightPanel.add(label2b,Constants.getConstraints(0,2,10));
	    rightPanel.add(text2b,Constants.getConstraints(1,2,50));
	    rightPanel.add(label3b,Constants.getConstraints(0,3,10));
	    rightPanel.add(text3b,Constants.getConstraints(1,3,50));
	    rightPanel.add(label4b,Constants.getConstraints(0,4,10));
	    rightPanel.add(text4b,Constants.getConstraints(1,4,50));
	    rightPanel.add(label5b,Constants.getConstraints(0,5,10));
	    rightPanel.add(text5b,Constants.getConstraints(1,5,50));
	    rightPanel.add(label6b,Constants.getConstraints(0,6,10));
	    rightPanel.add(text6b,Constants.getConstraints(1,6,50));
	    rightPanel.add(label7b,Constants.getConstraints(0,7,10));
	    rightPanel.add(text7b,Constants.getConstraints(1,7,50));
	    rightPanel.add(label8b,Constants.getConstraints(0,8,10));
	    rightPanel.add(text8b,Constants.getConstraints(1,8,50));
	    
	//    Group
	  
	    JTabbedPane ruleDefinitionPane = new JTabbedPane();
	
	    this.setLayout(null);
	    this.add(optionsPanel);
	    
	    optionsPanel.setBounds(0,0,290,100);
	    
	    this.add(predefinedRulesPanel);
	    this.add(userDefinedRulesPanel);
		this.add(probabilisticRulesPanel);
		
		predefinedRulesPanel.setBounds(0,100,290,200) ;
		userDefinedRulesPanel.setBounds(0,100,290,150) ;
		probabilisticRulesPanel.setBounds(0,100,290,250) ;
		
		probabilisticRulesPanel.setVisible(false);
		userDefinedRulesPanel.setVisible(false);
		predefinedRulesPanel.setVisible(true);
	}
	
	
	
	

}
